package com.yehui.spring.aop;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.AbstractBeanFactoryPointcutAdvisor;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Role;

/**
 * @author 业徽
 * @date 2021/7/1
 */
@Configuration
public class SimpleBeanFactoryPointcutAdvisorDemo {

    private static final Logger logger = LoggerFactory.getLogger(SimpleBeanFactoryPointcutAdvisorDemo.class);

    /**
     * advisor 是不会进行代理的
     *
     * @return
     */
    @Bean(name = "myAdvisor")
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public SimpleBeanFactoryPointcutAdvisor advisor() {
        final SimpleBeanFactoryPointcutAdvisor advisor = new SimpleBeanFactoryPointcutAdvisor();

        advisor.setAdvice((MethodBeforeAdvice) (method, args, target) -> logger.info("my before advice. method={}, " +
                "args={}, target={}", method, args, target));

        return advisor;

    }

    public static class SimpleBeanFactoryPointcutAdvisor extends AbstractBeanFactoryPointcutAdvisor {
        /**
         * Get the Pointcut that drives this advisor.
         */
        @Override
        public Pointcut getPointcut() {
            return Pointcut.TRUE;
        }
    }

    @Override
    public String toString() {
        return "SimpleBeanFactoryPointcutAdvisor";
    }
}
